package com.github.caijh.mall.finance.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.caijh.mall.common.PageParams;
import com.github.caijh.mall.finance.dao.UserFundsMonitorDao;
import com.github.caijh.mall.finance.model.UserFundsMonitor;
import com.github.caijh.mall.finance.request.FundsMonitorUserSearchRequest;
import com.github.caijh.mall.finance.service.UserFundsMonitorService;
import com.github.caijh.mall.user.model.User;
import com.github.caijh.mall.user.service.UserService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * UserRechargeServiceImpl 接口实现
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Service
public class UserFundsMonitorServiceImpl extends ServiceImpl<UserFundsMonitorDao, UserFundsMonitor> implements UserFundsMonitorService {

    @Resource
    private UserFundsMonitorDao dao;

    @Autowired
    private UserService userService;

    /**
     * 佣金列表
     *
     * @return List<User>
     * @author Mr.Zhang
     * @since 2020-04-28
     */
    @Override
    public List<UserFundsMonitor> getFundsMonitor(FundsMonitorUserSearchRequest request, PageParams pageParams) {
        PageHelper.startPage(pageParams.getPage(), pageParams.getLimit());
        HashMap<String, Object> map = new HashMap<>();
        String keywords = null;
        if (!StringUtils.isBlank(request.getKeywords())) {
            keywords = "%" + request.getKeywords() + "%";
        }
        map.put("keywords", keywords);
        map.put("max", request.getMax());
        map.put("min", request.getMin());
        String sort = "desc";
        if (!StringUtils.isBlank(request.getSort())) {
            sort = request.getSort();
        }
        map.put("sort", sort);
        List<UserFundsMonitor> monitorList = dao.getFundsMonitor(map);
        List<Integer> spreadUidList = monitorList.stream().map(UserFundsMonitor::getSpreadUid).distinct().collect(Collectors.toList());
        if (CollUtil.isEmpty(spreadUidList)) {
            return monitorList;
        }
        HashMap<Integer, User> mapListInUid = userService.getMapListInUid(spreadUidList);
        for (UserFundsMonitor temp : monitorList) {
            if (ObjectUtil.isNotNull(temp.getSpreadUid())) {
                User user = mapListInUid.get(temp.getSpreadUid());
                if (ObjectUtil.isNotNull(user)) {
                    temp.setSpreadName(Optional.ofNullable(user.getNickname()).orElse(""));
                }
            }
        }
        return monitorList;
    }


}

